package com.geek.water.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geek.water.entity.CollectionStrategy;
import com.geek.water.mapper.CollectionStrategyMapper;
import com.geek.water.service.CollectionStrategyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 催缴策略服务实现类
 */
@Service
public class CollectionStrategyServiceImpl extends ServiceImpl<CollectionStrategyMapper, CollectionStrategy> implements CollectionStrategyService {

    @Override
    public List<CollectionStrategy> getActiveStrategies() {
        LambdaQueryWrapper<CollectionStrategy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectionStrategy::getIsActive, true)
                .orderByAsc(CollectionStrategy::getPriority)
                .orderByAsc(CollectionStrategy::getDaysOverdue);
        return list(wrapper);
    }

    /**
     * 获取所有催缴策略（包括禁用的）
     */
    public List<CollectionStrategy> getAllStrategies() {
        LambdaQueryWrapper<CollectionStrategy> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(CollectionStrategy::getPriority)
                .orderByAsc(CollectionStrategy::getDaysOverdue);
        return list(wrapper);
    }

    @Override
    public CollectionStrategy getStrategyByOverdueDays(Integer overdueDays) {
        LambdaQueryWrapper<CollectionStrategy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectionStrategy::getIsActive, true)
                .le(CollectionStrategy::getDaysOverdue, overdueDays)
                .orderByDesc(CollectionStrategy::getDaysOverdue)
                .last("LIMIT 1");
        return getOne(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createStrategy(CollectionStrategy strategy) {
        strategy.setCreateTime(LocalDateTime.now());
        strategy.setUpdateTime(LocalDateTime.now());
        strategy.setCreateBy("admin"); // 这里应该从当前登录用户获取
        strategy.setUpdateBy("admin");
        return save(strategy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStrategy(CollectionStrategy strategy) {
        strategy.setUpdateTime(LocalDateTime.now());
        strategy.setUpdateBy("admin"); // 这里应该从当前登录用户获取
        return updateById(strategy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toggleStrategyStatus(Long id, Boolean isActive) {
        CollectionStrategy strategy = new CollectionStrategy();
        strategy.setId(id);
        strategy.setIsActive(isActive);
        strategy.setUpdateTime(LocalDateTime.now());
        strategy.setUpdateBy("admin");
        return updateById(strategy);
    }

    @Override
    public boolean isStrategyUsed(Long id) {
        // 这里可以检查策略是否被催缴任务使用
        // 暂时返回false，表示可以删除
        // 后续可以根据实际业务需求完善这个检查逻辑
        return false;
    }
}
